<!DOCTYPE HTML>
<html>

<head>
    <title>产品分类 - 二手电子乐园</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <meta name="description" content="浏览各类二手电子产品" />
    <link rel="stylesheet" href="assets/css/main.css" />
    <link rel="stylesheet" href="assets/css/fontawesome-all.min.css" />
    <style>
        .category-header {
            text-align: center;
            margin-bottom: 2rem;
        }

        .category-title {
            font-size: 2.5rem;
            color: #333;
            margin-bottom: 1rem;
        }

        .category-description {
            font-size: 1.2rem;
            color: #666;
            max-width: 600px;
            margin: 0 auto;
        }

        .filters {
            background: #f8f9fa;
            padding: 1.5rem;
            border-radius: 8px;
            margin-bottom: 2rem;
        }

        .filter-group {
            margin-bottom: 1rem;
        }

        .filter-group:last-child {
            margin-bottom: 0;
        }

        .filter-label {
            font-weight: bold;
            margin-bottom: 0.5rem;
            display: block;
            color: #333;
        }

        .filter-options {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .filter-option {
            padding: 0.5rem 1rem;
            background: white;
            border: 1px solid #ddd;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 0.9rem;
        }

        .filter-option:hover,
        .filter-option.active {
            background: #e44c65;
            color: white;
            border-color: #e44c65;
        }

        .products-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 2rem;
            margin-top: 2rem;
        }

        .product-card {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
        }

        .product-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
        }

        .product-content {
            padding: 1.5rem;
        }

        .product-title {
            font-size: 1.2rem;
            font-weight: bold;
            color: #333;
            margin-bottom: 0.5rem;
            line-height: 1.4;
        }

        .product-description {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 1rem;
            line-height: 1.5;
        }

        .product-price {
            font-size: 1.5rem;
            font-weight: bold;
            color: #e44c65;
            margin-bottom: 1rem;
        }

        .product-seller {
            font-size: 0.9rem;
            color: #888;
            margin-bottom: 1rem;
        }

        .product-actions {
            display: flex;
            gap: 10px;
        }

        .btn-view {
            flex: 1;
            padding: 0.8rem;
            background: #e44c65;
            color: white;
            text-decoration: none;
            text-align: center;
            border-radius: 4px;
            transition: background 0.3s ease;
        }

        .btn-view:hover {
            background: #d63851;
            color: white;
        }

        .no-products {
            text-align: center;
            padding: 3rem;
            color: #666;
        }
    </style>
</head>

<body class="is-preload">
    <div id="page-wrapper">
        <div id="header-container"></div>

        <!-- Main Content -->
        <section id="main" class="wrapper style2">
            <div class="container" id="category-app">
                <!-- 分类标题 -->
                <div class="category-header" v-if="categoryInfo">
                    <h1 class="category-title">{{ categoryInfo.title }}</h1>
                    <p class="category-description">{{ categoryInfo.description }}</p>
                </div>

                <!-- 筛选器 -->
                <div class="filters">
                    <div class="filter-group">
                        <span class="filter-label">价格区间</span>
                        <div class="filter-options">
                            <span class="filter-option" :class="{ active: filters.priceRange === 'all' }"
                                @click="setFilter('priceRange', 'all')">全部</span>
                            <span class="filter-option" :class="{ active: filters.priceRange === '0-1000' }"
                                @click="setFilter('priceRange', '0-1000')">1000元以下</span>
                            <span class="filter-option" :class="{ active: filters.priceRange === '1000-3000' }"
                                @click="setFilter('priceRange', '1000-3000')">1000-3000元</span>
                            <span class="filter-option" :class="{ active: filters.priceRange === '3000-5000' }"
                                @click="setFilter('priceRange', '3000-5000')">3000-5000元</span>
                            <span class="filter-option" :class="{ active: filters.priceRange === '5000+' }"
                                @click="setFilter('priceRange', '5000+')">5000元以上</span>
                        </div>
                    </div>

                    <div class="filter-group">
                        <span class="filter-label">成色</span>
                        <div class="filter-options">
                            <span class="filter-option" :class="{ active: filters.condition === 'all' }"
                                @click="setFilter('condition', 'all')">全部</span>
                            <span class="filter-option" :class="{ active: filters.condition === '全新' }"
                                @click="setFilter('condition', '全新')">全新</span>
                            <span class="filter-option" :class="{ active: filters.condition === '99新' }"
                                @click="setFilter('condition', '99新')">99新</span>
                            <span class="filter-option" :class="{ active: filters.condition === '95新' }"
                                @click="setFilter('condition', '95新')">95新</span>
                            <span class="filter-option" :class="{ active: filters.condition === '9成新' }"
                                @click="setFilter('condition', '9成新')">9成新</span>
                        </div>
                    </div>
                </div>

                <!-- 产品列表 -->
                <div class="products-grid" v-if="filteredProducts.length > 0">
                    <div class="product-card" v-for="product in filteredProducts" :key="product.id">
                        <img :src="product.image" :alt="product.title" class="product-image" />
                        <div class="product-content">
                            <h3 class="product-title">{{ product.title }}</h3>
                            <p class="product-description">{{ product.description }}</p>
                            <div class="product-price">¥{{ product.price }}</div>
                            <div class="product-seller">
                                卖家：{{ product.seller.name }} | {{ product.seller.location }}
                            </div>
                            <div class="product-actions">
                                <a :href="'product-detail.html?id=' + product.id" class="btn-view">查看详情</a>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 无产品提示 -->
                <div class="no-products" v-else-if="products.length === 0">
                    <h3>暂无相关产品</h3>
                    <p>该分类下暂时没有产品，请稍后再来查看。</p>
                </div>

                <!-- 筛选无结果 -->
                <div class="no-products" v-else>
                    <h3>没有符合条件的产品</h3>
                    <p>请尝试调整筛选条件。</p>
                </div>
            </div>
        </section>

        <div id="footer-container"></div>
    </div>

    <!-- Scripts -->
    <script src="utils/loader.js"></script>
    <script>
        window.addEventListener('scriptsLoaded', function () {
            // 加载页头和页脚组件
            if (typeof fetchHtml !== 'undefined') {
                fetchHtml.fetchAll();
            }

            // 初始化 petite-vue
            if (typeof PetiteVue !== 'undefined') {
                console.log('PetiteVue 可用，开始初始化');

                // 等待数据加载完成后初始化Vue应用
                function initializeApp() {
                    if (!window.siteData) {
                        console.log('数据未加载，继续等待...');
                        setTimeout(initializeApp, 100);
                        return;
                    }

                    console.log('数据已加载，开始初始化Vue应用');

                    // 获取URL参数中的分类类型
                    const urlParams = new URLSearchParams(window.location.search);
                    const categoryType = urlParams.get('type') || 'phone';
                    console.log('分类类型:', categoryType);

                    // 分类映射
                    const categoryMap = {
                        'phone': 'phones',
                        'camera': 'cameras',
                        'digital': 'digital',
                        'fridge': 'refrigerators'
                    };

                    const mappedCategory = categoryMap[categoryType];
                    console.log('映射分类:', mappedCategory);

                    // 初始化数据
                    let categoryInfo = null;
                    let products = [];
                    let filteredProducts = [];

                    if (mappedCategory && window.siteData.categoryData[mappedCategory]) {
                        categoryInfo = window.siteData.categoryData[mappedCategory];
                        console.log('分类信息:', categoryInfo);

                        // 获取该分类的产品
                        const allProducts = [
                            ...window.siteData.featuredProducts,
                            ...window.siteData.recommendedProducts
                        ];
                        console.log('所有产品:', allProducts.length);

                        products = allProducts.filter(product =>
                            product.category === mappedCategory
                        );
                        console.log('筛选后产品:', products.length, products);

                        filteredProducts = [...products]; // 初始显示所有产品

                        // 更新页面标题
                        document.title = `${categoryInfo.title} - 二手电子乐园`;
                    } else {
                        console.error('未找到对应的分类', mappedCategory, window.siteData?.categoryData);
                    }

                    // 创建Vue应用
                    PetiteVue.createApp({
                        // 使用初始化的数据
                        categoryType: categoryType,
                        categoryInfo: categoryInfo,
                        products: products,
                        filteredProducts: filteredProducts,
                        filters: {
                            priceRange: 'all',
                            condition: 'all',
                            brand: 'all'
                        },

                        setFilter(type, value) {
                            this.filters[type] = value;
                            this.applyFilters();
                        },

                        applyFilters() {
                            let filtered = [...this.products];

                            // 价格筛选
                            if (this.filters.priceRange !== 'all') {
                                filtered = filtered.filter(product => {
                                    const price = parseInt(product.price);
                                    switch (this.filters.priceRange) {
                                        case '0-1000':
                                            return price < 1000;
                                        case '1000-3000':
                                            return price >= 1000 && price < 3000;
                                        case '3000-5000':
                                            return price >= 3000 && price < 5000;
                                        case '5000+':
                                            return price >= 5000;
                                        default:
                                            return true;
                                    }
                                });
                            }

                            // 成色筛选
                            if (this.filters.condition !== 'all') {
                                filtered = filtered.filter(product =>
                                    product.description.includes(this.filters.condition)
                                );
                            }

                            this.filteredProducts = filtered;
                            console.log('筛选完成，产品数量:', this.filteredProducts.length);
                        }
                    }).mount('#category-app');

                    console.log('Vue应用已挂载，初始产品数量:', filteredProducts.length);
                }

                // 开始初始化
                initializeApp();
            } else {
                console.error('PetiteVue 不可用');
            }
        });
    </script>
</body>

</html>